package cst.a720yyq.zyfypt_720yyq.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a720yyq.zyfypt_720yyq.ArticalActivity;
import cst.a720yyq.zyfypt_720yyq.Bean.TcaseBean;
import cst.a720yyq.zyfypt_720yyq.Bean.VideoBean;
import cst.a720yyq.zyfypt_720yyq.CaseActivity;
import cst.a720yyq.zyfypt_720yyq.Judgment.Judgement;
import cst.a720yyq.zyfypt_720yyq.R;

/**
 * Created by NNDMII on 2017/5/24.
 */

public class TcaseAdapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private List<TcaseBean> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }
    public TcaseAdapter(Context context, List<TcaseBean> list)
    {
        this.inflater = LayoutInflater.from(context);
        this.list=list;
        this.context=context;
    }

    public TcaseAdapter(Context context)
    {
        this.inflater = LayoutInflater.from(context);
        this.context=context;
    }


    public void setList(List<TcaseBean> l){
        list=l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.item_tcase,parent,false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TcaseBean bean = list.get(position);
        if(bean==null) return;
        final ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.thumb.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.null_img));
        viewHolder.name.setText(Judgement.getLimitLengthString(bean.getName(),23,"..."));
        viewHolder.insert_time.setText(bean.getUpdate_time());
        viewHolder.description.setText(Judgement.getLimitLengthString(bean.getDescription(),28,"..."));
        //viewHolder.itemView.setTag(bean);
//        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition();
//                    mOnItemClickListener.onItemClick(viewHolder.itemView, position);
                    Intent intent;
                    intent = new Intent(context,CaseActivity.class);//// TODO: 2017/5/27
                    intent.putExtra("id",list.get(position).getId());
                    context.startActivity(intent);
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = viewHolder.getLayoutPosition();

//                    mOnItemClickListener.onItemLongClick(viewHolder.itemView, position);
                    return false;
                }
            });
//        }
        //使用同样开发团队的Picasso支持包进行图片加载，由于接口中返回的img路径不是全的，所以需要加上网站前缀
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+bean.getThumb())
                .placeholder(R.drawable.null_img)
                .into(viewHolder.thumb);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return  0;
        else
            return list.size();
    }






    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView insert_time;
        TextView description;
        ImageView thumb;

        public ViewHolder(View itemView) {
            super(itemView);
            thumb = (ImageView) itemView
                    .findViewById(R.id.tcase_thumb);
            name = (TextView) itemView
                    .findViewById(R.id.tcase_name);
            insert_time = (TextView) itemView
                    .findViewById(R.id.tcase_insert_time);
            description = (TextView) itemView
                    .findViewById(R.id.tcase_description);

        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
